﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Security;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using System.Xml.Linq;
using Water.Model;
using Newtonsoft.Json;
using WaterApp.Common;

namespace WaterApp
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            string url = WebapiUrl.weburl()+"GetMenusForuser/" + Membership.GetUser();
            var requestJson = JsonConvert.SerializeObject(new MenuTree());
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
                //.Result.Content.ReadAsStringAsync().Result;
            //tb01.Text = responseJson;
            //return;
            IList<MenuTree> menulist = JsonConvert.DeserializeObject<IList<MenuTree>>(responseJson);
            //var menulist = JsonConvert.DeserializeObject<IList<Water.Model.Menu>>(responseJson);
            if (menulist == null)
            {
                return;
            }
            var query = from menu in menulist 
                        where menu.PNodeID == "0"
                        select menu;
            StringBuilder menubuil = new StringBuilder();
            #region foreach Menulist
            foreach (Water.Model.MenuTree menuitem in query)
            {
                menubuil.Append("<li class=\"closed\"><span class=\"folder\">" + menuitem.Title + "</span>");
                var query1 = from menu1 in menulist where menu1.PNodeID == menuitem.NodeID select menu1;
                menubuil.Append("   <ul>");
                foreach (Water.Model.MenuTree menuitem1 in query1)
                {
                    var query2 = from menu2 in menulist where menu2.PNodeID == menuitem1.NodeID select menu2;
                    if (query2.Count() == 0)
                    {
                        menubuil.Append("	   <li><a href=\"" + menuitem1.URL + "\" target=\"mframe\"><span class=\"file\">" + menuitem1.Title + "</span></a></li>");
                    }
                    else
                    {
                        menubuil.Append("<li class=\"closed\"><span class=\"folder\">" + menuitem1.Title + "</span>");
                        menubuil.Append("   <ul>");
                        foreach (Water.Model.MenuTree menuitem2 in query2)
                        {
                            menubuil.Append("	   <li><a href=\"" + menuitem2.URL + "\" target=\"mframe\"><span class=\"file\">" + menuitem2.Title + "</span></a></li>");
                        }
                        menubuil.Append("   </ul>");
                        menubuil.Append("</li>");
                    }                    
                }
                menubuil.Append("   </ul>");
                menubuil.Append("</li>");
            }
            #endregion
            browser.InnerHtml = menubuil.ToString();     
        }
    }

}